(() => {
	let ele = {}

	ele.nickname = $('#nickname')//昵称
	ele.mobile = $('#mobile')//手机号
	ele.invitedcode = $('#invitedcode')//邀请码
	ele.weixin = $('#weixin')//微信
	ele.QQcode = $('#QQcode')//QQ
	ele.zoneService = $('#zoneService')//游戏区服

	// 昵称弹框
	ele.openNick = $('#openNick')//昵称
	ele.changeNickname = $('#changeNickname')//昵称
	ele.modal_nick = $('#modal-nick')
	ele.nickCancell = $('#nickCancell')
	ele.nickDeter = $('#nickDeter')

	//打开昵称弹框
	ele.openNick.onclick = () => {
		ele.modal_nick.style.display = 'block'
	}
	//关闭昵称弹框
	ele.nickCancell.onclick = () => {
		ele.modal_nick.style.display = 'none'
	}
	// 确定昵称修改
	ele.nickDeter.onclick = () => {
		let fd = ele.changeNickname.value;
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.USEREDIT,
			method: 'POST',
			header: {
				Authorization: token
			},
			data: {
				nick_name: fd
			},
			success: res => {
				if (res.code == 1) {
					init()
					openTips(res.msg)
				} else {
					openTips(res.msg)
				}
			}
		});
	}


	//  我的微信号 
	ele.openWeixin = $('#openWeixin')
	ele.modal_weixin = $('#modal-weixin')
	ele.changeweixin = $('#changeweixin')
	ele.weixinCancell = $('#weixinCancell')
	ele.weixinDeter = $('#weixinDeter')

	//打开我的微信号弹框
	ele.openWeixin.onclick = () => {
		ele.modal_weixin.style.display = 'block'
	}
	//关闭我的微信号弹框
	ele.weixinCancell.onclick = () => {
		ele.modal_weixin.style.display = 'none'
	}
	// 确定我的微信号修改
	ele.weixinDeter.onclick = () => {
		let fd = ele.changeweixin.value;
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.USEREDIT,
			method: 'POST',
			header: {
				Authorization: token
			},
			data: {
				wechat: fd
			},
			success: res => {
				if (res.code == 1) {
					init()
					openTips(res.msg)
				} else {
					openTips(res.msg)
				}
			}
		});
	}

	//  我的QQ号 
	ele.openQQ = $('#openQQ')
	ele.modal_qq = $('#modal-qq')
	ele.changeqq = $('#changeqq')
	ele.qqCancell = $('#qqCancell')
	ele.qqDeter = $('#qqDeter')

	//打开我的QQ号弹框
	ele.openQQ.onclick = () => {
		ele.modal_qq.style.display = 'block'
	}
	//关闭我的QQ号弹框
	ele.qqCancell.onclick = () => {
		ele.modal_qq.style.display = 'none'
	}
	// 确定的QQ号修改
	ele.qqDeter.onclick = () => {
		let fd = ele.changeqq.value;
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.USEREDIT,
			method: 'POST',
			header: {
				Authorization: token
			},
			data: {
				qq: fd
			},
			success: res => {
				if (res.code == 1) {
					init()
					openTips(res.msg)
				} else {
					openTips(res.msg)
				}
			}
		});
	}

	init()
	function init() {
		getUserInfo(res => {
			if (res.server_name && res.role_name) {
				ele.bangding.innerHTML = '<span class="border">' + '已绑定</span>'
			}
			$('#fileImg').src = res.head_pic
			fileImg.style.opacity = 1
			ele.nickname.innerHTML = res.nick_name;
			ele.mobile.innerHTML = res.mobile.substr(0, 3) + '****' + res.mobile.substr(7, 4);
			ele.invitedcode.innerHTML = res.invite_code
			ele.weixin.innerHTML = res.wechat || '暂无'
			ele.QQcode.innerHTML = res.qq || '暂无'
			ele.zoneService.innerHTML = (res.server_name || '暂无区服') + ' | ' + (res.role_name || '暂无')

			ele.changeNickname.value = res.nick_name;
			ele.changeqq.value = res.qq || '暂未设置'
			ele.changeweixin.value = res.wechat || '暂未设置'


		})
	}

	// 换头像
	let inp = document.getElementById('fileInp');
	let fileImg = document.getElementById('fileImg');

	inp.onchange = function () {
		let inpObj = inp.files[0]; // 拿到上传文件的属性
		var form = new FormData();
		form.append('file', inpObj);
		if (inpObj) {
			let windowURL = window.URL || window.webkitURL;
			let img = document.getElementById('preview');
			if (inp && inpObj) {
				// createObjectURL(inpObj)  创建一个文件的临时地址
				let dataURL = windowURL.createObjectURL(inpObj);
				toBase64(dataURL);
			}

		}

		let token = getCookie("token");
		var form = new FormData();
		form.append('head_pic', inpObj);
		const xhr = new XMLHttpRequest()
		xhr.open('POST', API_PATH.CHANGEAVATAR)
		xhr.setRequestHeader('Authorization', token);
		xhr.setRequestHeader('ContentType', "multipart/form-data");
		xhr.onreadystatechange = function () {
			if (xhr.readyState !== 4) {
				return
			}
			if (xhr.status >= 200 && xhr.status < 300) {
				let res = JSON.parse(xhr.responseText)
				openTips(res.msg)
			}
		}
		//图片大小验证
		var fileSize = 0;
		var fileMaxSize = 3072;//3M
		if (inpObj) {
			fileSize = inpObj.size;
			var size = fileSize / 1024;
			console.log(size, fileMaxSize);
			if (size > fileMaxSize) {
				openTips("文件大小不能大于3M！");
				return false;
			} else {
				xhr.send(form)
			}
		} else {
			return false;
		}
	}

	function toBase64(imgSrc) {
		var canvas = document.createElement("canvas");
		canvas.width = 34;
		canvas.height = 34;
		let context = canvas.getContext("2d");
		context.rect(0, 0, canvas.width, canvas.height);
		context.fillStyle = "#fff";
		context.fill();

		let myImage = new Image();
		myImage.src = imgSrc; //背景图片  你自己本地的图片或者在线图片
		myImage.crossOrigin = 'Anonymous';
		let base64 = null;
		myImage.onload = () => {
			context.drawImage(myImage, 0, 0, 34, 34);
			base64 = canvas.toDataURL("image/png");

			// 等图片转换成base64之后 更新图片
			fileImg.setAttribute('src', base64);
			fileImg.style.opacity = 1
		}
	}



	// 温馨提示
	ele.modal_tips = $('#modal-tips')
	ele.tips_close = $('#tips-close')

	// 关闭温馨提示
	ele.tips_close.onclick = () => {
		ele.modal_tips.style.display = 'none'
	}

	// 绑定游戏区服和角色
	ele.modal_binding = $('#modal-binding')
	ele.bangding = $('#bangding')
	ele.binding_close = $('#binding-close')
	ele.bindingList = $('#bindingList')
	ele.empty = $('#empty')
	// 打开弹框

	ele.bangding.onclick = () => {
		getList2();
		ele.modal_binding.style.display = 'block'
	}
	ele.binding_close.onclick = () => {
		ele.modal_binding.style.display = 'none'
	}

	// 切换绑定账号
	function getList2() {
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.USERGAMEINFO,
			method: 'GET',
			header: {
				Authorization: token
			},
			success: res => {
				if (res.code == 1) {
					drawPropList2(res.data)
				}
				else openTips(res.msg);
			}
		});
	}
	// 渲染等级奖励列表
	function drawPropList2(data) {
		if (data.length == 0) {
			ele.empty.style.display = 'block'
		} else {
			ele.empty.style.display = 'none'
		}
		let html = '';
		for (let i = 0; i < data.length; i++) {
			let lv = data[i];
			let status = '';
			let txt = ''
			if (getCookie('server_id') != 0) {
				// console.log(lv.id, getCookie('server_id'));
				if (lv.server_id == getCookie('server_id')) {
					status = 'Noreceived'
					txt = '已绑定'
				} else {
					status = 'Noreceive'
					txt = '绑定'
				}
			} else {
				status = 'Noreceive'
				txt = '绑定'
			}

			html += '<div class="nobpx">' +
				'<div class="noleft">' +
				'<p class="n1">' + lv.server_name + '</p>' +
				'<p class="n2">' + lv.role_name + '</p>' +
				'</div>' +
				'<button class="' + status + '" data="' + lv.server_id + '">' +
				'<span class="rebox  Noreceive-' + lv.server_id + '">' + txt + '</span>' +
				'</button>' +
				'</div>'
		}
		ele.bindingList.innerHTML = html;
		bindReceive2();
	}
	// 绑定游戏信息角色按钮
	function bindReceive2() {
		let btns = $('.Noreceive');
		for (let i = 0; i < btns.length; i++) {
			btns[i].onclick = function () {
				receiveReward2(this.getAttribute('data'));
			}
		}
		let btnds = $('.Noreceived');
		for (let i = 0; i < btnds.length; i++) {
			btnds[i].onclick = function () {
				ele.modal_tips.style.display = 'block'
			}
		}
	}

	// 绑定游戏信息角色
	function receiveReward2(lv) {
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.BINDGAME,
			method: 'POST',
			data: {
				server_id: lv
			},
			header: {
				Authorization: token
			},
			success: res => {
				if (res.code == 1) {
					let btn = $('.Noreceive-' + lv)[0];
					btn.innerHTML = "已绑定"
					btn.classList.remove('Noreceive');
					btn.classList.add('Noreceived');
					btn.onclick = () => {
						openTips('您已领取过该奖励');
					};
					init()
				} else {
					openTips(res.msg);
				}
			}
		})
	}


})();